use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct IceDialMessage {
    #[serde(rename = "candidates")]
    pub candidates: Vec<String>,
    #[serde(rename = "ufrag")]
    pub ufrag: String,
    #[serde(rename = "pwd")]
    pub pwd: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct IceDialResponseMessage {
    #[serde(rename = "candidates")]
    pub candidates: Vec<String>,
    #[serde(rename = "ufrag")]
    pub ufrag: String,
    #[serde(rename = "pwd")]
    pub pwd: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "type", content = "data")]
pub enum IceMessage {
    #[serde(rename = "dial")]
    Dial(IceDialMessage),
    #[serde(rename = "dial_response")]
    DialResponse(IceDialResponseMessage),
}

impl IceMessage {}

impl From<&String> for IceMessage {
    fn from(msg: &String) -> Self {
        let msg_str = msg.as_str();
        serde_json::from_str(msg_str).expect("signal message unmarshal")
    }
}

impl From<&IceMessage> for String {
    fn from(msg: &IceMessage) -> Self {
        serde_json::to_string(msg).expect("signal message marshal")
    }
}
